// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ResourceServiceImp.java

package com.iphaeton.lms.business.resource;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.resource.Resource;
import com.iphaeton.lms.pojo.resource.Sort;
import com.iphaeton.persistence.IPersistenceService;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.*;

// Referenced classes of package com.iphaeton.lms.business.resource:
//            IResourceService

public class ResourceServiceImp extends PersistentServiceSupport
    implements IResourceService
{

    public ResourceServiceImp()
    {
    }

    public List getSpecifiedResource(Pagination pageInfo)
        throws BusinessException
    {
        return getSpecifiedItems(pageInfo);
    }

    public Resource findResourceById(String id)
        throws BusinessException
    {
        Resource res = null;
        try
        {
            res = (Resource)hbpservice.get(id, com.iphaeton.lms.pojo.resource.Resource.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u67E5\u8BE2\u8D44\u6E90\u5931\u8D25!");
        }
        return res;
    }

    public void removeResource(String id)
        throws BusinessException
    {
        try
        {
            hbpservice.remove(id, com.iphaeton.lms.pojo.resource.Resource.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u8D44\u6E90\u5931\u8D25:" + e.getMessage());
        }
    }

    public Resource persisteRes(Resource r)
        throws BusinessException
    {
        try
        {
            r = (Resource)hbpservice.store(r);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6301\u4E45\u5316\u8D44\u6E90\u5931\u8D25:" + e.getMessage());
        }
        return r;
    }

    public Pagination getSortResource(String id, int valid, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(r) from Resource r ";
        String hqlQueryStr = "from Resource r  ";
        if(valid != 0)
        {
            selectHql = selectHql + " where r.valid=" + valid;
            hqlQueryStr = hqlQueryStr + " where r.valid=" + valid;
            if(id != null && !"".equals(id))
            {
                selectHql = selectHql + " and r.sort='" + id + "'";
                hqlQueryStr = hqlQueryStr + " and r.sort='" + id + "'";
            }
        } else
        if(id != null && !"".equals(id))
        {
            selectHql = selectHql + " where r.sort='" + id + "'";
            hqlQueryStr = hqlQueryStr + " where r.sort='" + id + "'";
        }
        hqlQueryStr = hqlQueryStr + "  order by r.create_time desc ";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8D44\u6E90\u5206\u9875\u5217\u8868\u5931\u8D25!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public List getSort()
        throws BusinessException
    {
        String hql = "from Sort Sort";
        List list = null;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u8D44\u6E90\u5206\u7C7B\u5931\u8D25!");
        }
        return list;
    }

    public Document getSortTree(String type)
        throws BusinessException
    {
        Document doc = DocumentHelper.createDocument();
        Element element = doc.addElement("tree");
        List list = getSort();
        for(int i = 0; i < list.size(); i++)
        {
            Sort cs = (Sort)list.get(i);
            Element csElement = element.addElement("tree");
            csElement.addAttribute("text", cs.getName());
            if(type.equals("admin"))
                csElement.addAttribute("action", "../../resourceTxAction.do?method=getSortResource&id=" + cs.getId());
            else
            if(type.equals("user"))
                csElement.addAttribute("action", "../../resourceTxAction.do?method=userSortResource&id=" + cs.getId());
            csElement.addAttribute("icon", "");
            csElement.addAttribute("openIcon", "");
            csElement.addAttribute("target", "operateFrame");
            csElement.addAttribute("expended", "false");
        }

        return doc;
    }

    public void removeSort(String id)
        throws BusinessException
    {
        try
        {
            hbpservice.remove(id, com.iphaeton.lms.pojo.resource.Sort.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u5171\u4EAB\u5B66\u4E60\u8D44\u6E90\u5206\u7C7B\u65F6\u51FA\u9519!");
        }
    }

    public Sort findSortById(String id)
        throws BusinessException
    {
        Sort s;
        try
        {
            s = (Sort)hbpservice.get(id, com.iphaeton.lms.pojo.resource.Sort.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u901A\u8FC7ID\u67E5\u627E\u5171\u4EAB\u8D44\u6E90\u5206\u7C7B\u5931\u8D25!");
        }
        return s;
    }

    public Sort updateSort(Sort s)
        throws BusinessException
    {
        try
        {
            hbpservice.store(s);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u66F4\u65B0\u5B66\u4E60\u8D44\u6E90\u5206\u7C7B\u5931\u8D25!");
        }
        return s;
    }

    public Sort addCatalog(String name, String catalog)
        throws BusinessException
    {
        Sort sort = new Sort();
        sort.setName(name);
        sort.setCatalog(catalog);
        try
        {
            sort = (Sort)hbpservice.store(sort);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u65B0\u589E\u8D44\u6E90\u5206\u7C7B\u5931\u8D25!");
        }
        return sort;
    }

    private static Logger logger;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */
    static Class class$2; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.resource.ResourceServiceImp.class.getName());
    }
}
